package com.xxxy.df.baseui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class A1Activity extends AppCompatActivity {
    Button btnRight,btnError,btnFind;
    TextView tvResult;
    boolean flag; // 标记是否查看过答案

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_a1);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        init(); // 初始化界面组件
    }

    private void init(){
        // 绑定界面组件
        btnRight = findViewById(R.id.btn_right);
        btnError = findViewById(R.id.btn_error);
        btnFind = findViewById(R.id.btn_find);
        tvResult = findViewById(R.id.tv_result_trans);

        // 设置错误答案按钮点击事件
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag){
                    tvResult.setText("很遗憾，答案错误！"+"你已经查看过答案！");
                }else {
                    tvResult.setText("很遗憾，答案错误！");
                }
                // 禁用答题按钮
                btnRight.setEnabled(false);
                btnError.setEnabled(false);
            }
        });

        // 设置正确答案按钮点击事件
        btnError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag){
                    tvResult.setText("恭喜你，答案正确！"+"你已经查看过答案！");
                }else {
                    tvResult.setText("恭喜你，答案正确！");
                }
                // 禁用答题按钮
                btnRight.setEnabled(false);
                btnError.setEnabled(false);
            }
        });

        // 查看答案按钮点击事件
        btnFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(A1Activity.this,B1Activity.class);
                // 传递答案标识到B1Activity
                intent.putExtra("answer",false);
                // 启动B1Activity并等待返回结果
                startActivityForResult(intent,1);
            }
        });
    }

    // 处理从B1Activity返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode==RESULT_OK){
            flag = data.getBooleanExtra("flag",false); // 获取是否查看过答案的标记
        }
    }
}
